package com.ruoyi.web.controller.pinko;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.PinkoCurrencybasicfieldsLog;
import com.ruoyi.system.service.IPinkoCurrencybasicfieldsLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PinkoCurrencybasicfields;
import com.ruoyi.system.service.IPinkoCurrencybasicfieldsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 币种基础字段Controller
 * 
 * @author ruoyi
 * @date 2024-06-07
 */
@RestController
@RequestMapping("/system/currencybasicfields")
@Tag(name = "币种基础字段")
public class PinkoCurrencybasicfieldsController extends BaseController{

    @Autowired
    private IPinkoCurrencybasicfieldsService currencyBasicFieldsService;
    @Autowired
    private IPinkoCurrencybasicfieldsLogService currencyBasicFieldsLogService;

    /**
     * 查询币种基础字段列表
     */
    @PreAuthorize("@ss.hasPermi('system:currencybasicfields:list')")
    @GetMapping("/list")
    public TableDataInfo list(PinkoCurrencybasicfields pinkoCurrencybasicfields)
    {
        startPage();
        List<PinkoCurrencybasicfields> list = currencyBasicFieldsService.selectPinkoCurrencybasicfieldsList(pinkoCurrencybasicfields);
        return getDataTable(list);
    }

    /**
     * 导出币种基础字段列表
     */
    @PreAuthorize("@ss.hasPermi('system:currencybasicfields:export')")
    @Log(title = "币种基础字段", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PinkoCurrencybasicfields pinkoCurrencybasicfields)
    {
        List<PinkoCurrencybasicfields> list = currencyBasicFieldsService.selectPinkoCurrencybasicfieldsList(pinkoCurrencybasicfields);
        ExcelUtil<PinkoCurrencybasicfields> util = new ExcelUtil<PinkoCurrencybasicfields>(PinkoCurrencybasicfields.class);
        util.exportExcel(response, list, "币种基础字段数据");
    }

    /**
     * 获取币种基础字段详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:currencybasicfields:query')")
    @GetMapping(value = "/{id}")
    @Operation(summary = "信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(currencyBasicFieldsService.selectPinkoCurrencybasicfieldsById(id));
    }

    @GetMapping("/getlist")
    @Operation(summary = "信息")
    public TableDataInfo getList(){
        PinkoCurrencybasicfields params = new PinkoCurrencybasicfields();
        List<PinkoCurrencybasicfields> list = currencyBasicFieldsService.selectPinkoCurrencybasicfieldsList(params);

        return getDataTable(list);
    }
    /**
     * 新增币种基础字段
     */
    @PreAuthorize("@ss.hasPermi('system:currencybasicfields:add')")
    @Log(title = "币种基础字段", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PinkoCurrencybasicfields pinkoCurrencybasicfields)
    {

        PinkoCurrencybasicfields dto= pinkoCurrencybasicfields;
        dto.setUpdateDate(dto.getCreateDate());
        dto.setUpdater(dto.getCreator());
        dto.setUpdaterName(dto.getCreatorName());
        //判断字段是否已存在
        String strZDname=dto.getIsFieldsname().toString();
        PinkoCurrencybasicfields params = new PinkoCurrencybasicfields();
        params.setIsFieldsname(strZDname);
        List<PinkoCurrencybasicfields>entities=  currencyBasicFieldsService.selectPinkoCurrencybasicfieldsList(params);

        {
            if(entities.size()>0)
            {
                return error("该数据已存在，请重新输入！");
            }
            params = new PinkoCurrencybasicfields();
            params.setTraditionalchinese(strZDname);
            entities=  currencyBasicFieldsService.selectPinkoCurrencybasicfieldsList(params);
            if(entities.size()>0)
            {
                return error("该数据已存在，请重新输入！");
            }
            params = new PinkoCurrencybasicfields();
            params.setChinese(strZDname);
            entities=  currencyBasicFieldsService.selectPinkoCurrencybasicfieldsList(params);
            if(entities.size()>0)
            {
                return error("该数据已存在，请重新输入！");
            }
            params = new PinkoCurrencybasicfields();
            params.setEnglish(strZDname);
            entities=  currencyBasicFieldsService.selectPinkoCurrencybasicfieldsList(params);
            if(entities.size()>0)
            {
                return error("该数据已存在，请重新输入！");
            }
            params =new PinkoCurrencybasicfields();
            params.setJapanese(strZDname);
            entities=  currencyBasicFieldsService.selectPinkoCurrencybasicfieldsList(params);
            if(entities.size()>0)
            {
                return error("该数据已存在，请重新输入！");
            }
            params = new PinkoCurrencybasicfields();
            params.setRussian(strZDname);
            entities=  currencyBasicFieldsService.selectPinkoCurrencybasicfieldsList(params);
            if(entities.size()>0)
            {
                return error("该数据已存在，请重新输入！");
            }
            params = new PinkoCurrencybasicfields();
            params.setUkrainian(strZDname);
            entities=  currencyBasicFieldsService.selectPinkoCurrencybasicfieldsList(params);
            if(entities.size()>0)
            {
                return error("该数据已存在，请重新输入！");
            }
            params = new PinkoCurrencybasicfields();
            params.setKorean(strZDname);
            entities=  currencyBasicFieldsService.selectPinkoCurrencybasicfieldsList(params);
            if(entities.size()>0)
            {
                return error("该数据已存在，请重新输入！");
            }
            params = new PinkoCurrencybasicfields();
            params.setSpanish(strZDname);
            entities=  currencyBasicFieldsService.selectPinkoCurrencybasicfieldsList(params);
            if(entities.size()>0)
            {
                return error("该数据已存在，请重新输入！");
            }
        }
        currencyBasicFieldsService.insertPinkoCurrencybasicfields(dto);
        params = new PinkoCurrencybasicfields();
        params.setIsFieldsname(strZDname);
        entities=  currencyBasicFieldsService.selectPinkoCurrencybasicfieldsList(params);
        if(entities.size()>0) {
            dto.setId(entities.get(0).getId());
            saveactionlog(dto,"ADD","新增数据");
        }
        return success();
    }
    public  void saveactionlog(PinkoCurrencybasicfields dto,String action,String actionDesction )
    {

        PinkoCurrencybasicfieldsLog dtolog = new PinkoCurrencybasicfieldsLog();

        dtolog.setFatherId(dto.getId().toString());
        dtolog.setChinese(dto.getChinese());
        dtolog.setTraditionalchinese(dto.getTraditionalchinese());
        dtolog.setEnglish(dto.getEnglish());
        dtolog.setJapanese(dto.getJapanese());
        dtolog.setRussian(dto.getRussian());
        dtolog.setUkrainian(dto.getUkrainian());
        dtolog.setKorean(dto.getKorean());
        dtolog.setSpanish(dto.getSpanish());
        dtolog.setCreator(dto.getUpdater());
        dtolog.setCreateDate(dto.getUpdateDate());
        dtolog.setCreatorName(dto.getCreatorName());
        dtolog.setAction(action);
        dtolog.setActiondescription(actionDesction);
        dtolog.setIsFieldsname(dto.getIsFieldsname());
        currencyBasicFieldsLogService.insertPinkoCurrencybasicfieldsLog(dtolog);
    }
    /**
     * 修改币种基础字段
     */
    @PreAuthorize("@ss.hasPermi('system:currencybasicfields:edit')")
    @Log(title = "币种基础字段", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PinkoCurrencybasicfields pinkoCurrencybasicfields)
    {
        saveactionlog(pinkoCurrencybasicfields,"UPDATE","更新数据");
        return toAjax(currencyBasicFieldsService.updatePinkoCurrencybasicfields(pinkoCurrencybasicfields));
    }

    /**
     * 删除币种基础字段
     */
    @PreAuthorize("@ss.hasPermi('system:currencybasicfields:remove')")
    @Log(title = "币种基础字段", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        for (long idstr:ids) {
            PinkoCurrencybasicfields data = currencyBasicFieldsService.selectPinkoCurrencybasicfieldsById(idstr);
            if(data!=null&&data.getIsFieldsname().toString().length()>0){
                data.setUpdaterName("");
                saveactionlog(data,"DELETED","删除数据");
            }
        }
        return toAjax(currencyBasicFieldsService.deletePinkoCurrencybasicfieldsByIds(ids));
    }
}
